<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(sxREQ("reqtype") == "save"){	
	$errMsg .= puLinkMod();
}
$sSQL = "SELECT * FROM pu_link "
	." WHERE process_unit='".sxREQ('pu')."' AND name='".sxREQ('lnk')."'";
$objSDAO->query($sSQL,"selPuLk");
$row = $objSDAO->fetch("selPuLk");
sxRDef('new_name', $row['name']);
sxRDef('group', $row['group']);
sxRDef('sequence', $row['sequence']);
sxRDef('label', $row['label']);
sxRDef('tooltip', $row['tooltip']);
sxRDef('note', $row['note']);

//load processes 
$sSQL = "SELECT * FROM process_unit ORDER BY process_unit";
$objSDAO->query($sSQL, "puList");
$aValPU = array('- back -'=>'-- previous --','- self -'=>'-- self --');
while($rowPU = $objSDAO->fetch("puList")){
	$aValPU[$rowPU['process_unit']] = $rowPU['process_unit']." - ".$rowPU['description'];
}


$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language='JavaScript' type='text/javascript'>
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php 
echo getPuHeader(sxREQ('pu'));

$aCmd = array("save"=>array("pag"=>"javascript:objFrmSubm('save');",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
echo(commandsHTML($aCmd));	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');
echo getHtmlHidden('pu');
echo getHtmlHidden('lnk');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Link</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".getHtmlText('new_name',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Linked process-unit*</td>" .
			"<td class=\"text01\">");
echo getHtmlSelect('pu_linked',$aValPU);
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Group</td>" .
			"<td class=\"text01\">".getHtmlText('group',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Sequence</td>" .
			"<td class=\"text01\">".getHtmlText('sequence',4,8)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">label</td>" .
			"<td class=\"text01\">".getHtmlText('label',40,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Tool-tip</td>" .
			"<td class=\"text01\">".getHtmlTextArea('tooltip',100,3)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',100,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puLinkMod(){
 	global $objSDAO;
	$linkQuote = "`";
	$errMsg = "";
	$_REQUEST["new_name"] = trim(strtolower($_REQUEST["new_name"]));
	if(sxREQUEST("new_name")=='') {
		$errMsg .= "name missing<br>";
	}
	if(preg_match("/[^a-z0-9_]/",sxREQUEST("new_name"))) {
		$errMsg .= "name : only a-z 0-9 _<br>";
	}

	//check existence
	if (sxREQUEST('new_name') != sxREQUEST('lnk')) {
		$sSQL = "SELECT count(*) as num FROM pu_link " .
				"	WHERE process_unit='".sxRAS('process_unit')."' " .
						"	AND name='".sxRAS('new_name')."'";
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row['num'] != 0)
			$errMsg .= "name existing<br>";
	}

	if($errMsg != ""){return $errMsg;}
	if (trim(sxREQUEST('label'))=='') $_REQUEST['label'] = sxREQ('new_name');
	$sSQL = "UPDATE pu_link SET " .
			"pu_linked='".sxRAS("pu_linked")."', " .
			"`group`='".sxRAS("group")."', " .
			"sequence=".sxRInt('sequence').", " .
			"label='".sxRAS("label")."', " .
			"tooltip='".sxRAS("tooltip")."', " .
			"note='".sxRAS("note")."' " .
			"WHERE process_unit='".sxREQ('pu')."' AND name='".sxREQ('lnk')."'";	
	$objSDAO->query($sSQL,"upd");
	if (sxREQUEST('new_name') != sxREQUEST('lnk')) {
		$sSQL = "UPDATE pu_link SET name = '".sxRAS('new_name')."' " .
				"	WHERE process_unit='".sxRAS('pu')."' " .
				"  	AND name='".sxRAS('lnk')."'";
		$objSDAO->query($sSQL,"test");
	}
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
 }
